/**
 * 
 */
package com.kgcx.schedule.rabbitmq.consumer;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.kgcx.common.rabbitmq.consts.QueueConst;
import com.kgcx.core.utils.JsonUtil;
import com.kgcx.schedule.domain.ScheduleRecordDomain;
import com.kgcx.schedule.service.DriverStateService;
import com.kgcx.schedule.service.ScheduleRecordService;

/**
 * @author 袁进勇
 *
 */
@Component

public class DelayUnlockDriverConsumer {
    private static final Logger LOG = LoggerFactory.getLogger(DelayUnlockDriverConsumer.class);

    @Resource
    private ScheduleRecordService scheduleRecordService;
    @Resource
    private DriverStateService driverStateService;

    // @RabbitHandler
    @RabbitListener(queues = QueueConst.SCHEDULE_UNLOCK_DRIVER, errorHandler = "custRabbitListenerErrorHandler")
    @Transactional(rollbackFor = Exception.class)
    public void unlockDriver(String recordJson) {
        LOG.info("收到延时解锁司机的MQ消息。\n" + recordJson);
        ScheduleRecordDomain record = JsonUtil.toObject(recordJson, ScheduleRecordDomain.class);
        scheduleRecordService.archiveScheduleRecord(record);
        if (DriverStateService.SWITCH) {
            driverStateService.unlockDriver(record.getDriver_id());
        }
    }
}
